﻿using System.Collections.Generic;
using System.Text;
using RAP.Framework.Libary.DomTemplateBuilder.Element.Switch;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.SwitchCase
{
	public class SwitchHandler : DynamicHandler
    {
		public new Switch Element
		{
			get
			{
				return base.Element as Element.Switch.Switch;
			}
			set
			{
				base.Element = value;
			}
		}

        protected override void DoRenderChildNodes ( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
            foreach ( var ele in this.Element.ChildNodes )
            {
				var handler = ele.Handler as CaseHandler;
                if ( handler != null )
                {
                    if ( handler.IsCondition ( rootParamObject, parentParamObject, context ) )
                    {
                        handler.DoRender ( writer, rootParamObject, parentParamObject, context );
                        if ( context.ToBoolean ( "IsBreak", false ) )
                        {
                            context.Remove ( "IsBreak" );
                            return;
                        }
                    }
                }
            }
			//如果没有任何Case节点处理并调用Break，则调用Default处理节点
			var defaultEle = this.Element.Default;
			if ( defaultEle != null )
			{
				defaultEle.Handler.DoRender ( writer, rootParamObject, parentParamObject, context );
			}
        }
    }
}
